<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>门禁二维码生成器</title>
    <script src="https://cdn.jsdelivr.net/npm/qrcode@1.5.3/build/qrcode.min.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            text-align: center;
            color: #333;
            margin-bottom: 30px;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #555;
        }
        select, input {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
        }
        button {
            background-color: #007bff;
            color: white;
            padding: 12px 30px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            margin: 10px 5px;
        }
        button:hover {
            background-color: #0056b3;
        }
        .qr-container {
            text-align: center;
            margin-top: 30px;
            padding: 20px;
            background-color: #f8f9fa;
            border-radius: 5px;
        }
        #qrcode {
            margin: 20px 0;
        }
        .qr-data {
            background-color: #e9ecef;
            padding: 15px;
            border-radius: 5px;
            margin-top: 15px;
            word-break: break-all;
            font-family: monospace;
        }
        .store-info {
            background-color: #d4edda;
            padding: 15px;
            border-radius: 5px;
            margin-bottom: 20px;
            border-left: 4px solid #28a745;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>🚪 门禁二维码生成器</h1>
        
        <div class="form-group">
            <label for="storeSelect">选择门店：</label>
            <select id="storeSelect" onchange="updateStoreInfo()">
                <option value="store_001">门店1 (store_001)</option>
                <option value="store_002">门店2 (store_002)</option>
            </select>
        </div>
        
        <div class="store-info" id="storeInfo">
            <strong>门店信息：</strong><br>
            <span id="storeDetails"></span>
        </div>
        
        <div class="form-group">
            <label for="customData">自定义二维码数据（JSON格式）：</label>
            <textarea id="customData" rows="6" style="width: 100%; padding: 10px; border: 1px solid #ddd; border-radius: 5px; font-family: monospace;"></textarea>
        </div>
        
        <div style="text-align: center;">
            <button onclick="generateQRCode()">🔄 生成二维码</button>
            <button onclick="downloadQRCode()">💾 下载二维码</button>
            <button onclick="copyQRData()">📋 复制数据</button>
        </div>
        
        <div class="qr-container" id="qrContainer" style="display: none;">
            <h3>生成的门禁二维码</h3>
            <div id="qrcode"></div>
            <div class="qr-data">
                <strong>二维码数据：</strong><br>
                <span id="qrData"></span>
            </div>
        </div>
    </div>

    <script>
        const storeConfigs = {
            'store_001': {
                storeId: 'store_001',
                storeName: '宠物服务门店1',
                deviceId: 'access_control_001',
                deviceName: '门店1门禁'
            },
            'store_002': {
                storeId: 'store_002',
                storeName: '宠物服务门店2',
                deviceId: 'access_control_002',
                deviceName: '门店2门禁'
            }
        };
        
        let currentQRData = '';
        
        function updateStoreInfo() {
            const storeId = document.getElementById('storeSelect').value;
            const config = storeConfigs[storeId];
            
            document.getElementById('storeDetails').innerHTML = `
                门店ID: ${config.storeId}<br>
                门店名称: ${config.storeName}<br>
                设备ID: ${config.deviceId}<br>
                设备名称: ${config.deviceName}
            `;
            
            // 更新自定义数据
            const qrData = {
                type: 'door_access',
                storeId: config.storeId,
                deviceId: config.deviceId,
                storeName: config.storeName,
                deviceName: config.deviceName,
                version: '1.0'
            };
            
            document.getElementById('customData').value = JSON.stringify(qrData, null, 2);
        }
        
        function generateQRCode() {
            const customData = document.getElementById('customData').value.trim();
            
            if (!customData) {
                alert('请输入二维码数据');
                return;
            }
            
            try {
                // 验证JSON格式
                JSON.parse(customData);
                currentQRData = customData;
                
                // 清空之前的二维码
                document.getElementById('qrcode').innerHTML = '';
                
                // 生成二维码
                QRCode.toCanvas(document.getElementById('qrcode'), customData, {
                    width: 300,
                    height: 300,
                    margin: 2,
                    color: {
                        dark: '#000000',
                        light: '#FFFFFF'
                    }
                }, function (error) {
                    if (error) {
                        console.error(error);
                        alert('生成二维码失败：' + error.message);
                    } else {
                        document.getElementById('qrData').textContent = customData;
                        document.getElementById('qrContainer').style.display = 'block';
                        console.log('二维码生成成功');
                    }
                });
                
            } catch (error) {
                alert('JSON格式错误：' + error.message);
            }
        }
        
        function downloadQRCode() {
            const canvas = document.querySelector('#qrcode canvas');
            if (!canvas) {
                alert('请先生成二维码');
                return;
            }
            
            const storeId = document.getElementById('storeSelect').value;
            const link = document.createElement('a');
            link.download = `门禁二维码_${storeId}.png`;
            link.href = canvas.toDataURL();
            link.click();
        }
        
        function copyQRData() {
            if (!currentQRData) {
                alert('请先生成二维码');
                return;
            }
            
            navigator.clipboard.writeText(currentQRData).then(() => {
                alert('二维码数据已复制到剪贴板');
            }).catch(err => {
                console.error('复制失败:', err);
                alert('复制失败，请手动复制');
            });
        }
        
        // 页面加载时初始化
        window.onload = function() {
            updateStoreInfo();
        };
    </script>
</body>
</html>